﻿@model Matrix.MaKaYunShang.Model.DiscountBase
@{
    ViewBag.Title = "立减规则修改";
    ViewBag.PathToBtn = "/SysConfig/DiscountBase";
    ViewBag.TextBtn = "返回立减规则列表";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                立减规则修改
            </div>
            <div class="panel-body">
                <div class="row">
                    @using (Html.BeginForm("SysPayDiscountConfig", "SysConfig", FormMethod.Post, new { id = "frmCreate", enctype = "multipart/form-data" }))
                    {
                        @Html.AntiForgeryToken()
                        @Html.ValidationSummary(true)
                        <div class="col-lg-12">
                            <fieldset>
                                <div class="form-group col-md-2">
                                    <label>标题</label>
                                    @Html.TextBoxFor(model => model.Title, new { @class = "form-control", placeholder = "标题" })
                                </div>
                                <div class="form-group col-md-2">
                                    <label>时间间隔</label>
                                    @Html.TextBoxFor(model => model.TimeSpanCount, new { @class = "form-control", placeholder = "单位(天)" })
                                </div>
                                <div class="form-group col-md-2">
                                    <label>时间间隔类型</label>
                                    @Html.DropDownListFor(model => model.TimeSpanType, new List<SelectListItem>()
            {
            new SelectListItem(){Text="日",Value="0"}, new SelectListItem(){Text="自然周",Value="1"}, new SelectListItem(){Text="自然月",Value="2"},new SelectListItem(){Text="自然年",Value="3"}
            }, new { @class = "form-control" }
            )
                                </div>
                                <div class="form-group col-md-2">
                                    <label>用户类型</label>
                                    @Html.DropDownListFor(model => model.UserType, new List<SelectListItem>()
            {
            new SelectListItem(){Text="不限制",Value="0"}, new SelectListItem(){Text="平台用户",Value="1"}, new SelectListItem(){Text="店铺用户",Value="2"}
            }, new { @class = "form-control" }
            )
                                </div>
                                <div class="form-group col-md-2">
                                    <label>用户可享受立减次数</label>
                                    @Html.TextBoxFor(model => model.ConsumerTotalTimes, new { @class = "form-control", placeholder = "用户可享受立减次数" })
                                </div>
                            </fieldset>
                            <div style=" overflow:auto; width:100%;" id="DiscountTimesProbabilityDiv">
                                <fieldset id="DiscountTimesProbability">
                                    @foreach (var item in Model.DiscountTimesProbabilityList)
                                    {
                                        var num = @Convert.ToInt32(Model.DiscountTimesProbabilityList.IndexOf(item)) + 1;
                                        <div class='form-group col-md-2'><label style='color:blue'>第<label id='SerialNum' name='SerialNum'>@num</label>次（百分比）</label><input type='text' value="@item.DiscountPercent" id='DiscountPercent' name='DiscountPercent' class='form-control' placeholder='几率（百分比）'></div>
                                    }
                                </fieldset>
                            </div>
                            <fieldset id="MinusConfigureHtml">
                                @if (Model.SysPayDiscountConfigList.Any())
                                {
                                    foreach (var item in Model.SysPayDiscountConfigList)
                                    {
                                        <div id="MinusConfigureDiv">
                                            <div class="form-group col-md-12"></div>
                                            <div class="form-group col-md-2">
                                                <label>立减金额区间最小值</label>
                                                <input type="text" value="@item.MinPayDiscount" id="MinPayDiscount" name="MinPayDiscount" class="form-control" placeholder="立减金额区间最小值" />
                                            </div>
                                            <div class="form-group col-md-2">
                                                <label>立减金额区间最大值</label>
                                                <input type="text" value="@item.MaxPayDiscount" id="MaxPayDiscount" name="MaxPayDiscount" class="form-control" placeholder="立减金额区间最大值" />
                                            </div>
                                            <div class="form-group col-md-2">
                                                <label>权重</label>
                                                <input type="text" value="@item.DiscountProbability" id="DiscountProbability" name="DiscountProbability" class="form-control" placeholder="权重" />
                                            </div>
                                            @if (Model.SysPayDiscountConfigList.IndexOf(item) == 0)
                                            {
                                                <div class="form-group col-md-2">
                                                    <label>   </label>
                                                    <input type="button" value="添加" class="form-control" id="MinusConfigureBtn" style=" width:70px" />
                                                </div>
                                            }
                                            else
                                            {

                                                <div class="form-group col-md-2">
                                                    <label>   </label>
                                                    <input type="button" value="删除" onclick='delMinusConfigure(this)' class="form-control red" id="delMinusConfigureBtn" style=" width:70px" />
                                                </div>
                                            }
                                        </div>
                                    }
                                }
                                else
                                {
                                    <div id="MinusConfigureDiv">
                                        <div class="form-group col-md-12"></div>
                                        <div class="form-group col-md-2">
                                            <label>立减金额区间最小值</label>
                                            <input type="text" id="MinPayDiscount" name="MinPayDiscount" class="form-control" placeholder="立减金额区间最小值" />
                                        </div>
                                        <div class="form-group col-md-2">
                                            <label>立减金额区间最大值</label>
                                            <input type="text" id="MaxPayDiscount" name="MaxPayDiscount" class="form-control" placeholder="立减金额区间最大值" />
                                        </div>
                                        <div class="form-group col-md-2">
                                            <label>权重</label>
                                            <input type="text" id="DiscountProbability" name="DiscountProbability" class="form-control" placeholder="权重" />
                                        </div>

                                        <div class="form-group col-md-2">
                                            <label>   </label>
                                            <input type="button" value="添加" class="form-control" id="MinusConfigureBtn" style=" width:70px" />
                                        </div>
                                    </div>
                                }
                            </fieldset>
                            <div class="col-md-12">
                                <button type="button" class="btn btn-primary" id="btnSave">保存设置</button>
                            </div>
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>
</div>
@section javascript{
    <script type="text/javascript">
        $(function() {
            $("#frmCreate").validate({
                rules: {
                    Title: { required: true },
                    MinPayDiscount: { required: true, min: 0.1, number: true },
                    MaxPayDiscount: { required: true, min: 0.1, number: true },
                    DiscountProbability: { required: true, min: 0.1, number: true },
                    ConsumerTotalTimes: { required: true, min: 1, max: 10, number: true },
                    TimeSpanCount: { required: true, min: 1, number: true }
                },
                messages: {
                    MinPayDiscount: { required: "请填写立减金额区间最小值" },
                    MaxPayDiscount: { required: "请填写立减金额区间最大值" },
                    DiscountProbability: { required: "请填写权重值" },
                    Title: { required: "请填写标题" },
                    TimeSpanCount: { required: "请填写时间间隔" }
                }
            });

            $("#btnSave").click(function() {
                if ($("#frmCreate").valid()) {
                    var isError = false;
                    var discountPercentStr = '';
                    $("#DiscountTimesProbability input").each(function(bt) {
                        var value1 = $(this).val();
                        if (value1 == null || value1 == '') {
                            discountPercentStr = '';
                            alertify.alert('请填写立减次数属性');
                            isError = true;
                            return false;
                        } else if (!/^[0-9]*[1-9][0-9]*$/.test(value1) || value1 < 1 || value1 > 100) {
                            alertify.alert('几率值只能是1-100之间的整数');
                            isError = true;
                            return false;
                        } else {
                            discountPercentStr += value1 + ",";
                        }
                    });
                    var minusConfigureStr = '';
                    $("#MinusConfigureHtml #MinusConfigureDiv").each(function() {
                        var minPayDiscountValue = parseFloat($(this).find("input").eq(0).val());
                        var maxPayDiscountValue = parseFloat($(this).find("input").eq(1).val());
                        var discountProbabilityValue = $(this).find("input").eq(2).val();
                        if (!/^-?\d+\.?\d{0,2}$/.test(minPayDiscountValue)) {
                            minusConfigureStr = '';
                            alertify.alert('请填写正确的立减金额区间最小值');
                            isError = true;
                            return false;
                        }
                        if (!/^-?\d+\.?\d{0,2}$/.test(maxPayDiscountValue)) {
                            minusConfigureStr = '';
                            alertify.alert('请填写正确的立减金额区间最大值');
                            isError = true;
                            return false;
                        }
                        if (!/^-?\d+\.?\d{0,2}$/.test(discountProbabilityValue)) {
                            minusConfigureStr = '';
                            alertify.alert('请填写正确的请填写权重值');
                            isError = true;
                            return false;
                        }
                        if (minPayDiscountValue == '' || minPayDiscountValue == null) {
                            minusConfigureStr = '';
                            alertify.alert('请填写立减金额区间最小值');
                            isError = true;
                            return false;
                        } else if (maxPayDiscountValue == null || maxPayDiscountValue == '') {
                            minusConfigureStr = '';
                            alertify.alert('请填写立减金额区间最大值');
                            isError = true;
                            return false;
                        } else if (discountProbabilityValue == null || discountProbabilityValue == '') {
                            minusConfigureStr = '';
                            alertify.alert('请填写权重值');
                            isError = true;
                            return false;
                        } else if (minPayDiscountValue > maxPayDiscountValue) {
                            alertify.error("立减金额最大值不可小于立减金额最小值");
                            isError = true;
                            return false;
                        } else {
                            minusConfigureStr += minPayDiscountValue + ',' + maxPayDiscountValue + ',' + discountProbabilityValue + '|';
                        }
                    });
                    if (isError) {
                        return false;
                    }
                    if (discountPercentStr != '' && minusConfigureStr != '') {
                        $.ajax({
                            type: 'post',
                            url: '@Url.Action("SysPayDiscountUpdate")',
                            data: $("#frmCreate").serialize() + "&discountPercentStr=" + discountPercentStr + "&minusConfigureStr=" + minusConfigureStr,
                            success: function(data) {
                                if (data.Status) {
                                    alertify.success("修改成功");
                                    location.href = "/SysConfig/DiscountBase";
                                } else {
                                    alertify.error(data.Message);
                                }
                            },
                            error: function() {
                                alertify.error("修改失败，请重试！");
                            }
                        });
                    }
                }
            });

            $("#TimeSpanType").change(function() {
                var timeSpanType = $("#TimeSpanType").val();
                if (timeSpanType == 0) {
                    $("#TimeSpanCount").attr("placeholder", "单位(天)");
                }
                if (timeSpanType == 1) {
                    $("#TimeSpanCount").attr("placeholder", "单位(周)");
                }
                if (timeSpanType == 2) {
                    $("#TimeSpanCount").attr("placeholder", "单位(月)");
                }
                if (timeSpanType == 3) {
                    $("#TimeSpanCount").attr("placeholder", "单位(年)");
                }
            });

            $("#ConsumerTotalTimes").keyup(function() {
                var discountTimesProbabilityCount = $("#DiscountTimesProbability").children("div").length;
                var text = "";
                var consumerTotalTimes = $("#ConsumerTotalTimes").val();
                if (consumerTotalTimes < 1 || consumerTotalTimes > 10) {
                    return alertify.alert("用户可享受立减次数必须是1-10之间");
                }
                if (discountTimesProbabilityCount < 1) {
                    for (var i = 2; i <= consumerTotalTimes; i++) {
                        text += "<div class='form-group col-md-2'><label style='color:blue'>第<label ID='SerialNum' name='SerialNum' > " + i + "</label>次（百分比）</label><input type='text' ID='DiscountPercent' name='DiscountPercent' class='form-control' placeholder='几率（百分比）'></div>";
                    }
                } else {
                    if (consumerTotalTimes > discountTimesProbabilityCount) {
                        for (var j = 1; j <= parseInt(consumerTotalTimes) - parseInt(discountTimesProbabilityCount); j++) {
                            var sum = parseInt(discountTimesProbabilityCount) + parseInt(j);
                            text += "<div class='form-group col-md-2'><label style='color:blue'>第<label ID='SerialNum' name='SerialNum' > " + sum + "</label>次（百分比）</label><input type='text' ID='DiscountPercent' name='DiscountPercent' class='form-control' placeholder='几率（百分比）'></div>";
                        }
                    }
                    if (discountTimesProbabilityCount > consumerTotalTimes) {

                        for (var j = 1; j <= parseInt(discountTimesProbabilityCount) - parseInt(consumerTotalTimes); j++) {
                            $("#DiscountTimesProbability").children().last("div").remove();
                        }
                    }
                }
                $("#DiscountTimesProbability").append(text);
            });

            $("#MinusConfigureBtn").click(function() {
                var text = "<div id='MinusConfigureDiv'><div class='form-group col-md-11'> </div>";
                text += " <div class='form-group col-md-2'>";
                text += "<label>立减金额区间最小值</label>";
                text += "<input type='text' id='MinPayDiscount' name='MinPayDiscount' class='form-control' placeholder = '立减金额区间最小值'>";
                text += "</div> <div class='form-group col-md-2'>  <label>立减金额区间最大值</label>";
                text += "<input type='text' id='MaxPayDiscount' name='MaxPayDiscount' class='form-control' placeholder = '立减金额区间最大值'>";
                text += " </div>     <div class='form-group col-md-2'>      <label>权重</label>";
                text += "<input type='text' id='DiscountProbability' name='DiscountProbability' class='form-control' placeholder = '权重'></div>";
                text += "<div class='form-group col-md-2'> <label>   </label>";
                text += "<input type='button' value='删除' onclick='delMinusConfigure(this)' class='form-control red' id='delMinusConfigureBtn' style=' width:70px'/> </div></div>  ";
                $("#MinusConfigureHtml").append(text);
            });
        });

        function delMinusConfigure(dd) {
            $(dd).parent().parent().attr("id", "MinusConfigureDiv").remove();
        }

    </script>
}